package com.xgst.land.activity.check

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.loper7.date_time_picker.DateTimeConfig
import com.loper7.date_time_picker.dialog.CardDatePickerDialog
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xgst.land.R
import com.xgst.land.databinding.ActivityInventoryRecordLayoutBinding
import com.xgst.land.utils.ProgressUtils.showProgress
import com.xgst.land.viewmodel.InventoryRecordViewModel

//盘点记录
class InventoryRecordActivity:BaseActivity<ActivityInventoryRecordLayoutBinding>(), OnItemClickListener, OnRefreshLoadMoreListener,
    View.OnClickListener {
    override fun viewBinding(): ActivityInventoryRecordLayoutBinding {
        return ActivityInventoryRecordLayoutBinding.inflate(layoutInflater)
    }

    private lateinit var model: InventoryRecordViewModel
    private var currentPage = 0
    private val adapter = InventoryRecordAdapter()
    private var startTime = ""
    private var endTime  = ""
    private var sampleName  = ""
    private var formName  = ""
    override fun init() {
        setTitle("盘点记录")
        model = ViewModelProvider(this)[InventoryRecordViewModel::class.java]

        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.recyclerView.adapter = adapter
        binding.refreshLayout.setOnRefreshLoadMoreListener(this)

        binding.tvStartTime.setOnClickListener(this)
        binding.tvEndTime.setOnClickListener(this)

        adapter.setOnItemClickListener(this)
        binding.btnStart.setOnClickListener {
            startTime = binding.tvStartTime.text.toString()
            endTime = binding.tvEndTime.text.toString()
            sampleName = binding.tvSampleType.text.toString()
            formName = binding.etProjectName.text.toString()
            if(startTime.isEmpty() || endTime.isEmpty()){
                showToast("请选择时间")
                return@setOnClickListener
            }
            showProgress()
            initData()
        }
    }

    @SuppressLint("SetTextI18n")
    fun initData(){
        model.request(
            currentPage = "$currentPage",
            startTime = startTime,
            endTime = endTime,
            sampleName = sampleName,
            formName = formName)
        {
            dismissProgress()
            binding.refreshLayout.finishRefresh()
            binding.refreshLayout.finishLoadMore()
            if(currentPage == 0){
                adapter.setNewInstance(it)
            }else{
                if(it != null){
                    adapter.addData(it)
                }
            }
            binding.tvCount.text = "查询总数量：${adapter.data.size}"
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
      /*  val item = adapter.getItem(position) as PagePD
        val intent = Intent(this,InventoryDetailActivity::class.java)
        intent.putExtra("id",item.id)
        startActivity(intent)*/
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        currentPage = 0
        initData()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        currentPage++
        initData()
    }

    override fun onClick(v: View?) {
        when(v){
            binding.tvStartTime -> {
                CardDatePickerDialog.builder(this)
                    .setTitle("选择开始时间")
                    .showBackNow(false)
                    .showFocusDateInfo(false)
                    .setThemeColor(getColor(R.color.blue))
                    .setBackGroundModel(CardDatePickerDialog.CUBE)
                    .setDisplayType(arrayListOf(
                        DateTimeConfig.YEAR,
                        DateTimeConfig.MONTH,
                        DateTimeConfig.DAY,
                        DateTimeConfig.HOUR,DateTimeConfig.MIN,DateTimeConfig.SECOND))
                    .setOnChoose {millisecond->
                        binding.tvStartTime.text = TimeUtils.millis2String(millisecond,"yyyy-MM-dd HH:mm:ss")
                    }.build().show()
            }

            binding.tvEndTime -> {
                CardDatePickerDialog.builder(this)
                    .setTitle("选择结束时间")
                    .showBackNow(false)
                    .showFocusDateInfo(false)
                    .setThemeColor(getColor(R.color.blue))
                    .setBackGroundModel(CardDatePickerDialog.CUBE)
                    .setDisplayType(arrayListOf(
                        DateTimeConfig.YEAR,
                        DateTimeConfig.MONTH,
                        DateTimeConfig.DAY,
                        DateTimeConfig.HOUR,DateTimeConfig.MIN,DateTimeConfig.SECOND))
                    .setOnChoose {millisecond->
                        binding.tvEndTime.text = TimeUtils.millis2String(millisecond,"yyyy-MM-dd HH:mm:ss")

                    }.build().show()
            }
        }
    }

}
